// FilePlugins.cpp

#include "StdAfx.h"

#include "../Agent/Agent.h"

#include "FilePlugins.h"
#include "PluginLoader.h"
#include "Common/String/StringUtils.h"

namespace Common {
namespace Plugin {

int CExtDatabase::FindExtInfoBig(const CString &ext)
{
  for (size_t i = 0; i < ExtBigItems.size(); i++)
    if (ExtBigItems[i]->Ext.CompareNoCase(ext) == 0)
      return i;
  return -1;
}

int CExtDatabase::FindPlugin(const CString &plugin)
{
  for (size_t i = 0; i < Plugins.size(); i++)
    if (Plugins[i]->Name.CompareNoCase(plugin) == 0)
      return i;
  return -1;
}

void CExtDatabase::Read()
{
  /*
  std::vector<CExtInfo> extItems;
  ReadInternalAssociations(extItems);
  */
  ReadFileFolderPluginInfoList(Plugins);
  /*
  for (int i = 0; i < extItems.size(); i++)
  {
    const CExtInfo &extInfo = extItems[i];
    CExtInfoBig extInfoBig;
    extInfoBig.Ext = extInfo.Ext;
    extInfoBig.Associated = false;
    for (int p = 0; p < extInfo.Plugins.Size(); p++)
    {
      int pluginIndex = FindPlugin(extInfo.Plugins[p]);
      if (pluginIndex >= 0)
        extInfoBig.PluginsPairs.Add(CPluginEnabledPair(pluginIndex, true));
    }
    ExtBigItems.Add(extInfoBig);
  }
  */
  for (size_t pluginIndex = 0; pluginIndex < Plugins.size(); pluginIndex++)
  {
    const auto &pluginInfo = Plugins[pluginIndex];

    CPluginLibrary pluginLibrary;
    CComPtr<IFolderManager> folderManager;

    if (pluginInfo->FilePath.IsEmpty())
	{
		Agent::CArchiveFolderManager * manager = new  CComObject<Agent::CArchiveFolderManager>;
		manager->FinalConstruct();
		folderManager = manager;
	}
    else if (pluginLibrary.LoadAndCreateManager(pluginInfo->FilePath,
         pluginInfo->ClassID, &folderManager) != S_OK)
      continue;
    CComBSTR extBSTR;
    if (folderManager->GetExtensions(&extBSTR) != S_OK)
      return;
    const CString ext2 = (const wchar_t *)extBSTR;
    CStringVector exts;
	String::SplitStringBySpace(ext2, exts);
    for (size_t i = 0; i < exts.size(); i++)
    {
      const CString &ext = exts[i];
      #ifdef UNDER_CE
      if (ext == _T("cab"))
        continue;
      #endif
      int index = FindExtInfoBig(ext);
      if (index < 0)
      {
		  std::shared_ptr<CExtInfoBig> extInfo = std::make_shared<CExtInfoBig>();
		extInfo->PluginsPairs.push_back(CPluginEnabledPair(pluginIndex, false));
        extInfo->Associated = false;
        extInfo->Ext = ext;
		ExtBigItems.push_back(extInfo);
      }
      else
      {
        auto &extInfo = ExtBigItems[index];
        int pluginIndexIndex = extInfo->FindPlugin(pluginIndex);
        if (pluginIndexIndex < 0)
			extInfo->PluginsPairs.push_back(CPluginEnabledPair(pluginIndex, false));
      }
    }
  }
}

void CExtDatabase::Save()
{
  /*
  std::vector<CExtInfo> extItems;
  for (int i = 0; i < ExtBigItems.size(); i++)
  {
    const CExtInfoBig &extInfoBig = ExtBigItems[i];
    CExtInfo extInfo;
    // extInfo.Enabled = extInfoBig.Associated;
    extInfo.Ext = extInfoBig.Ext;
    for (int p = 0; p < extInfoBig.PluginsPairs.Size(); p++)
    {
      CPluginEnabledPair pluginPair = extInfoBig.PluginsPairs[p];
      if (pluginPair.Enabled)
        extInfo.Plugins.Add(Plugins[pluginPair.Index].Name);
    }
    extItems.Add(extInfo);
  }
  WriteInternalAssociations(extItems);
  */
}

}}